#include "userinfowidget.h"
#include "mainwidget.h"

userInfoWidget::userInfoWidget(QWidget *parent,const UserInfo& userinfo)
    : QDialog(parent),userInfo(userinfo)
{
    // 1. 设置基本属性
    this->setFixedSize(320,150);
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    // 窗口被关闭时, 自动销毁这个对话框对象.
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(10,10,0,0);
    glayout->setAlignment(Qt::AlignLeft);
    glayout->setHorizontalSpacing(0);
    this->setLayout(glayout);

    // 3. 添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75,75);
    avatarBtn->setIconSize(QSize(75,75));
    avatarBtn->setIcon(userInfo.avatar);

    // 统一样式
    QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}";
    QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); }";
    btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }";
    int width = 100;
    int height = 30;

    // 4. 添加用户序号
    idTag = new QLabel();
    idTag->setText("序号:");
    idTag->setStyleSheet(labelStyle);
    idTag->setFixedSize(50,height);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setFixedSize(width,height);
    idLabel->setText(userInfo.userId);
    idLabel->setStyleSheet(labelStyle);
    idLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    // 5. 添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称:");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(50,height);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setFixedSize(width,height);
    nameLabel->setText(userInfo.nickname);
    nameLabel->setStyleSheet(labelStyle);
    nameLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("手机:");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(50,height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setFixedSize(width,height);
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setStyleSheet(labelStyle);
    phoneLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    // 7. 添加功能按钮
    applyBtn = new QPushButton();
    applyBtn->setText("添加好友");
    applyBtn->setFixedSize(80,30);
    applyBtn->setStyleSheet(btnStyle);
    applyBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(80,30);
    sendMessageBtn->setStyleSheet(btnStyle);
    sendMessageBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    delFriendBtn = new QPushButton();
    delFriendBtn->setText("删除好友");
    delFriendBtn->setFixedSize(80,30);
    delFriendBtn->setStyleSheet(btnStyle);
    delFriendBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    // 8. 添加上述内容到布局管理器中
    glayout->addWidget(avatarBtn,0,0,3,2);
    glayout->addWidget(idTag,0,2,1,2,Qt::AlignLeft);
    glayout->addWidget(idLabel,0,4,1,4);
    glayout->addWidget(nameTag,1,2,1,2,Qt::AlignLeft);
    glayout->addWidget(nameLabel,1,4,1,4);
    glayout->addWidget(phoneTag,2,2,1,2,Qt::AlignLeft);
    glayout->addWidget(phoneLabel,2,4,1,4);

    // 如果是自己的头像则不用显示按钮 // TODO
    glayout->addWidget(applyBtn,3,0,1,2);
    glayout->addWidget(sendMessageBtn,3,3,1,2);
    glayout->addWidget(delFriendBtn,3,6,1,2);
}

void userInfoWidget::leaveEvent(QEvent *event)
{
    (void)event;
    this->close();
    QDialog::leaveEvent(event);
}

void userInfoWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 3. 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(rect(), 15, 15);  // 圆角半径
    painter.fillPath(path, QColor("#F3F3F3"));
}
